package view;

import java.awt.BorderLayout;
import java.io.IOException;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

/**
 * 
 * @author Andrea Scripa
 * 
 */
public class SwingView extends AbstractView {
	private static final long serialVersionUID = 1L;

	// Bring in resource bundles...
	private static ResourceBundle myViewLabels = ResourceBundle
			.getBundle("resources.englishView");
	private static ResourceBundle myErrors = ResourceBundle
		.getBundle("resources.errorMessages");

	// Initialize GUI pieces
	private JFrame myFrame;
	private JMenuBar myMenuBar;
	private JTabbedPane myTabs;
	private JPanel myHistoryPanel;
	private JPanel mySettingsPanel;
	private JPanel myInputPanel;
	private Canvas myCanvas;
	private JTextArea myOutputBox;
	private ArrayList<TabInnerPanel> myInnerPanels;

	public SwingView() throws IOException {
		// Create the frame
		myFrame = new JFrame();
		myFrame.setTitle(myViewLabels.getString("ProgramTitle"));
		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Add menu bar
		MenuBar mb = new MenuBar();
		myMenuBar = mb.createMenuBar();
		myFrame.add(myMenuBar, BorderLayout.NORTH);

		// Add image tabs
		//myCanvas = getCanvas();
//		myInnerPanels = new ArrayList<TabInnerPanel>();
//		myInnerPanels.add(new TabInnerPanel(myCanvas));
		myTabs = new JTabbedPane();
		Tabs customTabs = new Tabs(myTabs, myInnerPanels, this);
	    myTabs.addTab(myViewLabels.getString("TabTitle"),
	                    customTabs.getInnerPanels().get(0));
	    myTabs.setTabComponentAt(0, customTabs);
		myFrame.add(myTabs, BorderLayout.WEST);

		// Add history panel and animation slider
		JPanel historyAndSettingsPanel = new JPanel(new BorderLayout());
		HistoryArea ha = new HistoryArea();
		myHistoryPanel = ha.historyBox();
		historyAndSettingsPanel.add(myHistoryPanel, BorderLayout.NORTH);
		SettingsPieces sp = new SettingsPieces();
		mySettingsPanel = sp.sliderComponent();
		historyAndSettingsPanel.add(mySettingsPanel, BorderLayout.SOUTH);
		myFrame.add(historyAndSettingsPanel, BorderLayout.EAST);

		// Add input and output boxes
		JPanel textAreaPanel = new JPanel(new BorderLayout());
		InputArea ia = new InputArea();
		myInputPanel = ia.createInputTextArea();
		textAreaPanel.add(myInputPanel, BorderLayout.SOUTH);
		OutputArea oa = new OutputArea();
		myOutputBox = oa.createOutputTextArea();
		textAreaPanel.add(myOutputBox, BorderLayout.NORTH);
		myFrame.add(textAreaPanel, BorderLayout.SOUTH);

		// Show it!
		myFrame.pack();
		myFrame.setVisible(true);
	}
	
	public void displayError(String message) {
		String errorFrame = myErrors.getString("ErrorFrame");
		JOptionPane.showMessageDialog(new JFrame(), message, errorFrame, JOptionPane.ERROR_MESSAGE);

	}
	
	public void displayOutput(String message)
	{
		OutputArea.updateOutputBox(message);
	}

	@Override
	public String getUserExpression() {
		InputArea ia = new InputArea();
		String expression = ia.getLastInput();
		return expression;

	}

	@Override
	public Canvas getCanvas() {
		
		return myCanvas;
	}
	
	@Override
	public void setCanvas(Canvas c)
	{
	    myCanvas = c;
	}
}
